package me.kafeitu.demo.activiti.web.oa.announce;

import me.kafeitu.demo.activiti.entity.oa.Announce;
import me.kafeitu.demo.activiti.entity.oa.Leave;
import me.kafeitu.demo.activiti.service.oa.announce.AnnounceManager;
import me.kafeitu.demo.activiti.service.oa.announce.AnnounceWorkflowService;
import me.kafeitu.demo.activiti.service.oa.leave.LeaveWorkflowService;
import me.kafeitu.demo.activiti.util.Page;
import me.kafeitu.demo.activiti.util.PageUtil;
import me.kafeitu.demo.activiti.util.UserUtil;
import me.kafeitu.demo.activiti.util.Variable;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.Map;

/**
 * 请假控制器，包含保存、启动流程
 *
 * @author HenryYan
 */
@Controller
@RequestMapping(value = "/oa/announce")
public class AnnounceController {

	@Autowired
	protected AnnounceWorkflowService workflowService;
	@Autowired
	protected TaskService taskService;
	
    private Logger logger = LoggerFactory.getLogger(getClass());
    
    @RequestMapping(value = {"apply", ""})
    public String createForm(Model model) {
        model.addAttribute("announce", new Announce());
        return "/oa/announce/apply";
    }
    
    /**
     * 启动请假流程
     *
     * @param announce
     */
    @RequestMapping(value="start",method=RequestMethod.POST)
    public String startWorkflow(Announce announce,RedirectAttributes redirectAttributes, HttpSession session){
    	User user = UserUtil.getUserFromSession(session);
    	announce.setUserId(user.getId());
    	Map<String, Object> variables = new HashMap<String, Object>();
    	ProcessInstance processInstance = workflowService.startWorkflow(announce, variables);
    	redirectAttributes.addFlashAttribute("message", "流程已启动，流程ID：" + processInstance.getId());
    	System.out.println(announce.getConnent());
    	return "redirect:/oa/announce/apply";
    }
    
    @RequestMapping(value="task")
    public ModelAndView taskList(HttpSession session, HttpServletRequest request){
	    ModelAndView mav = new ModelAndView("/oa/announce/taskList");
	    Page<Announce> page = new Page<Announce>(PageUtil.PAGE_SIZE);
	    int[] pageParams = PageUtil.init(page, request);
	    User user = UserUtil.getUserFromSession(session);
	    
	    String userId = user.getId();
	    workflowService.findTodoTasks(userId, page, pageParams);
	    mav.addObject("page",page);
	    return mav;
    }
    /**
     * 签收
     */
    @RequestMapping(value = "task/claim/{id}")
    public String claim(@PathVariable("id") String taskId, HttpSession session, RedirectAttributes redirectAttributes) {
    	String userId = UserUtil.getUserFromSession(session).toString();
    	taskService.claim(taskId, userId);
    	redirectAttributes.addAttribute("message", "任务已签收");
		return "redirect:/oa/announce/task";
	}
 

}
